<template>
  <div class="dashboard-container">
    <main>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="序号" width="180"> </el-table-column>
        <el-table-column prop="code" label="编号" width="280">
        </el-table-column>
        <el-table-column prop="name" label="名称" width="280">
        </el-table-column>
        <el-table-column prop="age" label="年龄" width="280"> </el-table-column>
        <el-table-column prop="address" label="地址"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <el-button @click="toReport(scope.row)" type="primary" size="small"
              >疾病上报</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </main>
    <el-dialog
      title="疾病上报"
      :visible="visible"
      :before-close="cancel"
      :destroy-on-close="true"
      width="30%"
    >
      <div style="display: flex">
        <span style="width: 100px">病害描述</span>
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="reportFrom.description"
        >
        </el-input>
      </div>
      <div style="display: flex; margin-top: 20px">
        <span style="width: 85px">上传图片</span>
        <img :src="fileList" alt="" />
        <el-upload
          action="#"
          list-type="picture-card"
          :before-upload="beforeUpload"
          :http-request="handleFileUpload"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="reportError()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getIllnessList, publishIllness, uploadImg } from "@/api/disease";
import { getTreeList } from "@/api/tree";
export default {
  data() {
    return {
      visible: false,
      description: "", //疾病描述
      fileList: "", //图片数组
      searchFrom: {
        currentPage: 1,
        pageSize: 100,
      },
      reportFrom: {
        code: "",
        name: "",
        treeInfoId: "",
        description: "",
        path: [], //图片路径
      },
      tableData: [],
    };
  },
  mounted() {
    this.getTreeList();
  },
  methods: {
    getTreeList() {
      getTreeList(this.searchFrom).then((res) => {
        this.tableData = res.data;
      });
    },
    toReport(row) {
      console.log(row.id);
      this.reportFrom.treeInfoId = row.id;
      this.reportFrom.code = row.code;
      this.reportFrom.name = row.name;

      this.visible = true;
    },
    reportError() {
      console.log(this.reportFrom);
      publishIllness(this.reportFrom).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: "上报成功",
            type: "success",
          });
        }
        this.cancel();
      });
    },
    beforeUpload(file) {
      // this.fileList = new Blob(file);
    },
    handleRemove() {
      this.fileList = "";
    },
    // 图片上传
    handleFileUpload(file) {
      this.loading = true;
      let formData = new FormData();
      formData.append("file", file.file);
      // 调用后端服务器的接口
      uploadImg(formData).then((resp) => {
        console.log(resp, "resp");
        this.reportFrom.path.push(resp.data);
      });
    },
    cancel() {
      this.visible = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
